//
//  SmartSortPopView.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/31.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "SmartSortPopView.h"
@interface SmartSortPopView(){
    BOOL _isHidden;
}

@property(nonatomic,strong)UIScrollView * contentScrollView;

@property(nonatomic,strong)SmartSortPopViewSortButton * selectedOptionButton;

@end
@implementation SmartSortPopView
-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.contentScrollView];
        self.height = self.contentScrollView.height;
    }
    return self;
}



-(NSArray*)options{
    if(_options == nil){
        _options = @[@"智能排序",@"销量由高到底",@"销量由低到高",@"价格由低到高",@"价格由高到低"];
    }
    return _options;
}

-(UIScrollView*)contentScrollView{
    if(_contentScrollView == nil){
        _contentScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _contentScrollView.showsVerticalScrollIndicator = NO;
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        CGFloat height = Category_height(44);
        [self.options enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            SmartSortPopViewSortButton * option = [[SmartSortPopViewSortButton alloc] initWithFrame:CGRectMake(0, idx*(height), self.width, height)];
            option.title = obj;
            [option addTarget:self action:@selector(sortButtonClick:)];
            [_contentScrollView addSubview:option];
            if(idx == 0){
                option.selected = YES;
                self.selectedOptionButton = option;
            }
            else{
                option.selected = NO;
            }
            
            switch (idx) {
                case 0:
                case 1:option.tag = 1;break;
                case 2:option.tag = 2;break;
                case 3:option.tag = 3;break;
                case 4:option.tag = 4;break;
                default:
                    break;
            }
            
            
        }];
        
        _contentScrollView.contentSize = CGSizeMake(0, self.options.count*height);
        _contentScrollView.height = self.options.count*height;
    }
    return _contentScrollView;
}

-(void)sortButtonClick:(UITapGestureRecognizer*)sender{
    if(self.selectedOptionButton){
        self.selectedOptionButton.selected = NO;
    }
    SmartSortPopViewSortButton * button = (SmartSortPopViewSortButton*)sender.view;
    button.selected = YES;
    self.selectedOptionButton = button;
    
    if([self.delegate respondsToSelector:@selector(SmartSortPopView:didSelectedsortType:)]){
        [self.delegate SmartSortPopView:self didSelectedsortType:sender.view.tag];
    }
}



-(void)hidden{
    if(!_isHidden){
        _isHidden = YES;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"CategoryPopViewHidden" object:nil userInfo:@{@"popView":self}];
    }

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self hidden];
}
@end



@interface SmartSortPopViewSortButton()

/**title*/
@property(nonatomic,strong)UILabel * textLabel;


@end


@implementation SmartSortPopViewSortButton

-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.textLabel.textColor = self.defaultColor;
        self.textLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.textLabel];
        
        
        self.underLine = [[UIView alloc] initWithFrame:CGRectZero];
        self.underLine.backgroundColor = self.underLineDefauleColor;
        [self addSubview:self.underLine];
        
        self.selected = NO;
    }
    return self;
}

-(void)setTitle:(NSString *)title{
    _title = title;
    self.textLabel.text = title;
    [self setNeedsLayout];
}



-(void)layoutSubviews{
    [super layoutSubviews];
    self.textLabel.size = [self.textLabel.text sizeInAre:MaxSize font:self.textLabel.font];
    self.textLabel.x = globalMargin;
    self.textLabel.centerY = self.height*0.5;
    
    self.underLine.x = self.textLabel.x;
    self.underLine.y = self.height-1.5;
    self.underLine.size = CGSizeMake(self.width - self.underLine.x, 0.5);
    
    
}

-(void)setSelected:(BOOL)selected{
    _selected = selected;
    self.textLabel.textColor = self.selected?self.selectedColor:self.defaultColor;
    self.underLine.backgroundColor = self.selected?self.selectedColor:self.underLineDefauleColor;
}

-(void)addTarget:(id)target action:(SEL)sel{
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:target action:sel]];
}


-(UIColor*)defaultColor{
    if(_defaultColor == nil){
        _defaultColor = HexColor(@"#222222");
    }
    return _defaultColor;
}

-(UIColor*)underLineDefauleColor{
    if(_underLineDefauleColor == nil){
        _underLineDefauleColor = HexColor(@"#EEEEEE");
    }
    return _underLineDefauleColor;
}

-(UIColor*)selectedColor{
    if(_selectedColor == nil){
        _selectedColor = HexColor(@"#3CB034");
    }
    return _selectedColor;
}
@end